package com.cn.music.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.cn.music.R;
import com.cn.music.databinding.ActivityRegisterBinding;
import com.cn.music.view.RegisterViewModel;
import com.cn.music.view.SendCodeViewModel;

public class RegisterActivity extends AppCompatActivity {

    private ActivityRegisterBinding binding;
    private RegisterViewModel registerViewModel;
    private SendCodeViewModel sendCodeViewModel;
    private String phone;
    private String password;
    private String nickname;
    private String code;
    private final static String TAG = "注册页面";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
        registerViewModel = new ViewModelProvider(this).get(RegisterViewModel.class);
        sendCodeViewModel = new ViewModelProvider(this).get(SendCodeViewModel.class);
        sendCode();
        register();
    }

    private void register() {
        binding.btnRegister.setOnClickListener(v -> {
            phone = binding.phoneInput.getText().toString();
            password = binding.passwordInput.getText().toString();
            code = binding.codeInput.getText().toString();
            nickname = binding.nicknameInput.getText().toString();
            Toast.makeText(this,password+phone+code+nickname,Toast.LENGTH_SHORT).show();

            if (TextUtils.isEmpty(phone)) {
                Toast.makeText(this,R.string.phone_input,Toast.LENGTH_SHORT).show();
            }if (TextUtils.isEmpty(password)) {
                Toast.makeText(this,R.string.password_input,Toast.LENGTH_SHORT).show();
            }if (TextUtils.isEmpty(code)) {
                Toast.makeText(this,R.string.code_input,Toast.LENGTH_SHORT).show();
            }if (TextUtils.isEmpty(nickname)) {
                Toast.makeText(this,R.string.nickname_input,Toast.LENGTH_SHORT).show();
            }if (phone.length() != 11){
                Toast.makeText(this,"请输入有效手机号码",Toast.LENGTH_SHORT).show();
            } else {
                registerViewModel.loadRegisterModel(phone, password, code, nickname).observe(this, registerModels -> {

//                    if (registerModels.get(0).getBody() == null) {
//                        Toast.makeText(this,"请求错误",Toast.LENGTH_SHORT).show();
//                    }else {
//                        String msg = registerModels.get(0).getBody().getMessage();
//
//                        if (registerModels.get(0).getBody().getCode() == 200) {
//                            Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();
//                        }else {
//                            Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
//                        }
//                    }
                    Log.e(TAG,"请求信息:"+registerModels.get(0));

                });
            }

        });

    }

    private void sendCode() {
        binding.sendCode.setOnClickListener(v -> {
            phone = binding.phoneInput.getText().toString();
            if (TextUtils.isEmpty(phone)) {
                Toast.makeText(this,R.string.phone_input,Toast.LENGTH_SHORT).show();
            }if (phone.length() != 11){
                Toast.makeText(this,"请输入有效手机号码",Toast.LENGTH_SHORT).show();
            } else {
                sendCodeViewModel.loadCodeModel(phone).observe(this, sendCodeModels -> {
                    String msg = sendCodeModels.get(0).getMessage();
                    if (sendCodeModels.get(0).getCode() == 200) {
                        Toast.makeText(this,"验证码发送成功",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

}